
# coding:utf-8

from utils.suger import *
from utils import dir2vec, limit


class AtkType(object):
    Phy = 1 #物理伤害
    Magic = 2 #魔法伤害
    Direct = 3 #直接伤害（不计算护甲和抗性）

class AtkWay(object):
    RayCast = 1 #线性检测，范围内伤害
    Bullet = 2 #发射子弹
    Aim = 3 #单体直接伤害

class UnitAttr(object):
    def __init__(self, unit):
        self.base = unit

        self.spd = 0 #移动速度
        self.force = 0 #加速推进力

        self.level = 1   #等级
        self.kill_exp = 0 #杀掉后能获取的经验值

        self.hp = 10
        self.hp_max = 10
        self.hp_regain = 0

        self.mp = 7
        self.mp_max = 7
        self.mp_regain = 0.4

        self.armor_phy = 0 #物理护甲
        self.armor_magic = 0 #魔抗

        self.atk = 0 #攻击力
        self.atk_type = AtkType.Phy #伤害类型
        self.atk_way = AtkWay.Bullet #攻击方式
        self.atk_range = 0 #攻击浮动范围
        self.atk_dist = 50 #攻击射程
        #self.atk_qianyao = 0.2 #前摇时间
        #self.atk_houyao = 0.1 #后摇时间
        self.atk_interval = 0.5 #攻击间隔

        def OnFire():
            if self['hp'] != 0:
                self['hp'] += self['hp_regain']
        t_hp_regain = flux.Timer(5)
        t_hp_regain.OnFire = OnFire

        def OnFire2():
            if self['mp'] != 0:
                self['mp'] += self['mp_regain']
        t_mp_regain = flux.Timer(5)
        t_mp_regain.OnFire = OnFire2

        t_hp_regain.Start()
        t_mp_regain.Start()

        self._t_hp_regain = t_hp_regain
        self._t_mp_regain = t_mp_regain

    def __getitem__(self, key):
        if key in vars(self):
            return vars(self)[key]

    def __setitem__(self, key, value):
        if key in vars(self):
            if key == 'hp':
                vars(self)[key] = limit(value, 0, self['hp_max'])
                if self['hp'] == 0:
                    self.base.Dead()
            elif key == 'mp':
                vars(self)[key] = limit(value, 0, self['mp_max'])
            else:
                vars(self)[key] = value

            if key in ('hp', 'mp', 'hp_max', 'mp_max', 'hp_regain', 'mp_gain'):
                smokesignal.emit('unit_hp_mp_changed', self.base)


class HeroAttr(UnitAttr):
    def __init__(self, unit):
        super(HeroAttr, self).__init__(unit)

        self.exp = 0 #当前经验值
        self.ap = 0
        self.sp = 0
